package com.youku.adSDK.model
{
   import com.youku.adSDK.ADContext;
   import com.youku.adSDK.model.vo.GroupData;
   import com.youku.adSDK.view.events.ADEvent;
   import com.youku.adSDK.Utils.AdFsoTools;
   import com.youku.adSDK.ADConstant;
   import flash.utils.getTimer;
   import flash.utils.setTimeout;
   import flash.events.Event;
   import com.youkuet.BCValidate;
   import flash.utils.clearTimeout;
   import flash.net.URLLoader;
   import com.youku.adSDK.YoukuAdEvent;
   import com.adobe.json.JSON;
   import flash.events.SecurityErrorEvent;
   import flash.events.IOErrorEvent;
   import com.youku.adSDK.model.vo.ADData;
   
   public class FrontProxy extends ADProxy
   {
      
      public function FrontProxy() {
         super();
      }
      
      private var _loadTimeOut:Boolean = false;
      
      private var _timeID:Number;
      
      private var _ltadrequest:Number = 0;
      
      private var _stadrequest:Number = 0;
      
      private var FRONT_AD_TIME_OUT:Number = 8000;
      
      private var _stadrequest_s:Number = 0;
      
      private var _context:ADContext;
      
      public function init(param1:ADContext) : void {
         this._context = param1;
         this._ltadrequest = 0;
         this._stadrequest = 0;
      }
      
      public function load(param1:Number) : void {
         var _loc2_:Object = null;
         var _loc3_:* = NaN;
         var _loc4_:String = null;
         var _loc5_:* = false;
         var _loc6_:* = false;
         var _loc7_:Object = null;
         var _loc8_:GroupData = null;
         if((this._context.isDvdPlay) || (this._context.allowBCSpecialType))
         {
            _eventDispather.dispatchEvent(new ADEvent(ADEvent.FRONT_DATA_IS_NULL));
            return;
         }
         if(param1 == 0)
         {
            if(this._context.vip != true)
            {
               _loc2_ = AdFsoTools.getADRefreshInfo();
               if((_loc2_) && (_loc2_.videoId))
               {
                  _loc3_ = new Date().getTime() - _loc2_.seedTime;
                  _loc4_ = this._context.videoId;
                  _loc5_ = _loc3_ < 300000 && _loc2_.videoId == _loc4_ && _loc2_.nowTime > -1 && _loc2_.nowTime < _loc2_.totalTime;
                  if(_loc5_)
                  {
                     _loc7_ = _loc2_.dataObj;
                     _loc8_ = super.parseGroupData(_loc7_);
                     _loc8_ = this.checkGroupData(_loc8_);
                     _eventDispather.dispatchEvent(new ADEvent(ADEvent.FRONT_DATA_LOADED,_loc8_));
                     return;
                  }
                  _loc6_ = _loc3_ >= 300000 || (_loc2_.videoId) && !(_loc2_.videoId == _loc4_);
                  if(_loc6_)
                  {
                     AdFsoTools.saveADRefreshInfo({});
                  }
               }
            }
         }
         super.loadData(ADConstant.FRONT_AD,ADConstant.FRONT_REQUEST_BASE,this._context,0,0,param1);
         this._stadrequest = getTimer();
         this._stadrequest_s = new Date().getTime();
         this._timeID = setTimeout(this.loadDataTimeOut,this.FRONT_AD_TIME_OUT);
      }
      
      override public function onLoaderComplete(param1:Event) : void {
         var groupData:GroupData = null;
         var e:Event = param1;
         if(this._loadTimeOut)
         {
            return;
         }
         this.recordLoadTime(false);
         clearTimeout(this._timeID);
         var jstr:String = URLLoader(e.currentTarget).data;
         var obj:Object = {};
         var bc:BCValidate = new BCValidate();
         var pd:Object = 
            {
               "videoId":this._context.videoId,
               "totalTime":this._context.totalTime
            };
         if(!bc.isCD(jstr,pd))
         {
            _eventDispather.dispatchEvent(new ADEvent(ADEvent.FRONT_DATA_INVALID));
            return;
         }
         if(jstr == null || jstr == "")
         {
            _eventDispather.dispatchEvent(new YoukuAdEvent(YoukuAdEvent.AD_LOAD_TIME,
               {
                  "isError":true,
                  "type":ADConstant.TYPE_AD_FRONT_REQUEST,
                  "errorCode":ADConstant.ERROR_CODE_DATA
               }));
            _eventDispather.dispatchEvent(new ADEvent(ADEvent.FRONT_DATA_IS_NULL));
            return;
         }
         try
         {
            obj = com.adobe.json.JSON.decode(jstr);
         }
         catch(erro:Error)
         {
            obj = null;
         }
         if(obj == null)
         {
            _eventDispather.dispatchEvent(new YoukuAdEvent(YoukuAdEvent.AD_LOAD_TIME,
               {
                  "isError":true,
                  "type":ADConstant.TYPE_AD_FRONT_REQUEST,
                  "errorCode":ADConstant.ERROR_CODE_DATA
               }));
            _eventDispather.dispatchEvent(new ADEvent(ADEvent.FRONT_DATA_IS_NULL));
            return;
         }
         if(!bc.isKeyOK(obj.K))
         {
            _eventDispather.dispatchEvent(new ADEvent(ADEvent.FRONT_DATA_INVALID));
            return;
         }
         groupData = super.parseGroupData(obj);
         if(!groupData)
         {
            _eventDispather.dispatchEvent(new ADEvent(ADEvent.FRONT_DATA_IS_NULL));
            return;
         }
         if(groupData.P != 1)
         {
            _eventDispather.dispatchEvent(new ADEvent(ADEvent.FRONT_DATA_IS_NULL));
            return;
         }
         if(!groupData.VAL && !groupData.JS && !groupData.SEED)
         {
            _eventDispather.dispatchEvent(new ADEvent(ADEvent.FRONT_DATA_IS_NULL));
            return;
         }
         if((groupData.VAL == null || groupData.VAL.length == 0) && (groupData.JS == null || groupData.JS.length == 0) && (groupData.SEED == null || groupData.SEED.length == 0))
         {
            _eventDispather.dispatchEvent(new ADEvent(ADEvent.FRONT_DATA_IS_NULL));
            return;
         }
         if((groupData.VAL) || (groupData.JS))
         {
            _eventDispather.dispatchEvent(new ADEvent(ADEvent.FRONT_DATA_LOADED,groupData));
            return;
         }
         if(groupData.SEED)
         {
            _eventDispather.dispatchEvent(new ADEvent(ADEvent.FRONT_SEED_DATA_LOADED,groupData));
            return;
         }
         _eventDispather.dispatchEvent(new ADEvent(ADEvent.FRONT_DATA_IS_NULL));
      }
      
      override protected function onLoaderSecurityError(param1:SecurityErrorEvent) : void {
         super.onLoaderSecurityError(null);
         _eventDispather.dispatchEvent(new YoukuAdEvent(YoukuAdEvent.AD_LOAD_TIME,
            {
               "isError":true,
               "type":ADConstant.TYPE_AD_FRONT_REQUEST,
               "ADConstant":ADConstant.ERROR_CODE_SECURITY
            }));
      }
      
      override protected function onLoaderIOError(param1:IOErrorEvent) : void {
         super.onLoaderIOError(null);
         _eventDispather.dispatchEvent(new YoukuAdEvent(YoukuAdEvent.AD_LOAD_TIME,
            {
               "isError":true,
               "type":ADConstant.TYPE_AD_FRONT_REQUEST,
               "errorCode":ADConstant.ERROR_CODE_IO
            }));
      }
      
      override public function onLoaderError(param1:Event) : void {
         clearTimeout(this._timeID);
         if(this._loadTimeOut)
         {
            return;
         }
         this.recordLoadTime(true);
         _eventDispather.dispatchEvent(new ADEvent(ADEvent.FRONT_DATA_LOAD_ERROR));
      }
      
      private function loadDataTimeOut() : void {
         this._loadTimeOut = true;
         _eventDispather.dispatchEvent(new YoukuAdEvent(YoukuAdEvent.AD_LOAD_TIME,
            {
               "isError":true,
               "type":ADConstant.TYPE_AD_FRONT_REQUEST,
               "errorCode":ADConstant.ERROR_CODE_TIMEOUT
            }));
         this.recordLoadTime(true);
         _eventDispather.dispatchEvent(new ADEvent(ADEvent.FRONT_DATA_LOAD_TIME_OUT));
      }
      
      private function recordLoadTime(param1:Boolean) : void {
         var _loc2_:Number = getTimer();
         this._ltadrequest = _loc2_ - this._stadrequest;
         _eventDispather.dispatchEvent(new ADEvent(ADEvent.FRONT_LOAD_TIME,
            {
               "lt":this._ltadrequest,
               "start":this._stadrequest_s,
               "end":new Date().getTime(),
               "error":param1
            }));
         if(param1 == false)
         {
            _eventDispather.dispatchEvent(new YoukuAdEvent(YoukuAdEvent.AD_LOAD_TIME,
               {
                  "isError":false,
                  "type":ADConstant.TYPE_AD_FRONT_REQUEST,
                  "errorCode":null
               }));
         }
      }
      
      private function checkGroupData(param1:GroupData) : GroupData {
         var _loc2_:GroupData = param1;
         if((_loc2_.JS) && _loc2_.JS.length > 0)
         {
            _loc2_.JS = this.checkADData(_loc2_.JS);
         }
         if((_loc2_.VAL) && _loc2_.VAL.length > 0)
         {
            _loc2_.VAL = this.checkADData(_loc2_.VAL);
         }
         if((_loc2_.SEED) && _loc2_.SEED.length > 0)
         {
            _loc2_.SEED = this.checkADData(_loc2_.SEED);
         }
         return _loc2_;
      }
      
      private function checkADData(param1:Array) : Array {
         var _loc4_:ADData = null;
         var _loc2_:Array = param1;
         var _loc3_:* = 0;
         while(_loc3_ < _loc2_.length)
         {
            _loc4_ = _loc2_[_loc3_] as ADData;
            if((_loc4_.SU) && _loc4_.SU.length > 0)
            {
               _loc4_.SU = this.checkArray(_loc4_.SU);
            }
            if((_loc4_.SUS) && _loc4_.SUS.length > 0)
            {
               _loc4_.SUS = this.checkArray(_loc4_.SUS);
            }
            if((_loc4_.SUE) && _loc4_.SUE.length > 0)
            {
               _loc4_.SUE = this.checkArray(_loc4_.SUE);
            }
            if((_loc4_.CUM) && _loc4_.CUM.length > 0)
            {
               _loc4_.CUM = this.checkArray(_loc4_.CUM);
            }
            if(_loc4_.CU)
            {
               _loc4_.CU = this.replaceSid(_loc4_.CU);
            }
            _loc3_++;
         }
         return _loc2_;
      }
      
      private function checkArray(param1:Array) : Array {
         var _loc4_:Object = null;
         var _loc2_:Array = param1;
         var _loc3_:* = 0;
         while(_loc3_ < _loc2_.length)
         {
            _loc4_ = _loc2_[_loc3_];
            if(_loc4_.hasOwnProperty("U"))
            {
               _loc4_.U = this.replaceSid(_loc4_.U);
            }
            _loc3_++;
         }
         return _loc2_;
      }
      
      private function replaceSid(param1:String) : String {
         var _loc2_:RegExp = new RegExp("sid=\\d+");
         var _loc3_:String = "sid=" + this._context.sid;
         var _loc4_:String = param1;
         _loc4_ = _loc4_.replace(_loc2_,_loc3_);
         return _loc4_;
      }
   }
}
